package com.emily.test.service.impl;

import com.emily.test.mapper.TDeptMapper;
import com.emily.test.model.TDept;
import com.emily.test.service.DeptService;
import com.emily.test.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;
import java.util.Random;

/**
 * @author ：zc
 * @date ：Created in 2019/3/27 10:37
 * @description：
 */
@Service
@Transactional(rollbackFor = {Exception.class})
public class DeptServiceImpl implements DeptService {
    @Resource
    TDeptMapper tDeptMapper;

    @Override
    public List<TDept> getDeptAllList() {
        return tDeptMapper.getDeptAllList();
    }

    @Override
    public List<TDept> getDeptList(String id) throws Exception {
        return tDeptMapper.getDeptList(id);
    }

    @Override
    public TDept getDept(Integer id) throws Exception {
        return tDeptMapper.getDept(id);
    }

    @Override
    public void delete(Integer id) throws Exception {
        tDeptMapper.delDept(id);
    }

    @Override
    public int insert(TDept o) {
        o.setUpdateTime(DateUtil.getNowStr());
        String deptCode;
        TDept num = new TDept();
        System.out.println(num);
        do {
            deptCode = o.getCompanyCode() +"-"+ new Random().nextInt((999999 - 111111 + 1));
            num  = tDeptMapper.selectDeptByCode(deptCode);
        } while (num!=null);
        o.setDeptCode(deptCode);
        return tDeptMapper.insertSelective(o);
    }

    @Override
    public void update(TDept o) {
        o.setUpdateTime(DateUtil.getNowStr());
        tDeptMapper.update(o);
    }
}
